﻿using Microsoft.Win32;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace RandomSelectTool.Resources.SubPage
{
    public partial class StudentClass : Page
    {
        public StudentClass()
        {
            InitializeComponent();
        }

        private void ImportCourseSchedule_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Excel Files|*.xlsx;*.xls|All Files|*.*";
            if (openFileDialog.ShowDialog() == true)
            {
                LoadCourseScheduleFromExcel(openFileDialog.FileName);
            }
        }

        private void LoadCourseScheduleFromExcel(string filePath)
        {
            ClearOldCourseInfo();

            FileInfo fileInfo = new FileInfo(filePath);
            if (!fileInfo.Exists)
            {
                MessageBox.Show("未找到指定的Excel文件！");
                return;
            }

            using (ExcelPackage package = new ExcelPackage(fileInfo))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; // 假设课程表在第一个工作表中

                // 遍历课程表的单元格，读取课程信息并显示在相应的位置
                for (int row = 1; row <= worksheet.Dimension.End.Row; row++)
                {
                    if (row == 1 || row == 2 || row == 5 || row == 8 || row == 11) // 排除指定的行
                        continue;

                    for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
                    {
                        if (col == 1) // 排除第一列
                            continue;

                        string courseName = worksheet.Cells[row, col].Value?.ToString();
                        if (!string.IsNullOrEmpty(courseName))
                        {
                            TextBlock textBlock = new TextBlock
                            {
                                Text = courseName,
                                HorizontalAlignment = HorizontalAlignment.Center,
                                VerticalAlignment = VerticalAlignment.Center,
                                FontSize = 16,
                                Foreground = System.Windows.Media.Brushes.White
                            };

                            // 将行号和列号减去排除的行数和列数，以匹配在Grid中的位置
                            int gridRow = row - (row > 2 ? 2 : 1) + 1; // 添加1来整体向下移动一行
                            int gridColumn = col - 1;

                            Grid.SetRow(textBlock, gridRow);
                            Grid.SetColumn(textBlock, gridColumn);
                            Class.Children.Add(textBlock);
                        }
                    }
                }
            }
        }
        private void ClearOldCourseInfo()
        {
            // 创建临时集合保存要删除的元素
            List<UIElement> elementsToRemove = new List<UIElement>();

            // 遍历 Class.Children，并添加要删除的元素到临时集合中
            foreach (UIElement element in Class.Children)
            {
                if (element is TextBlock)
                {
                    elementsToRemove.Add(element);
                }
            }

            // 从 Class.Children 中移除临时集合中的元素
            foreach (UIElement element in elementsToRemove)
            {
                Class.Children.Remove(element);
            }
        }
    }
}
